(function () {
    if (!auth()) return

    checkInput('.step-two')
    $(".form input").on("change", function (e) {
        checkInput('.step-two')
    })

    // 获取验证码
    $(".valid-btn").on("click", function(e) {
        var phone = $(".step-two .phone").val()
        if (phone) {
            $.ajax({
                url: 'https://healthcarddemo.tengmed.com/index.php?c=healthcard&a=getSMSCode',
                data: {
                    phone: phone
                },
                dataType: 'json',
                type: 'post',
                success: function (res) {
                    if (res.retcode === 0) {
                        $("#validnumber").val(res.smsCode)
                        checkInput('.step-two')
                    } else {
                        alert(res.retmsg)
                    }
                }
            })
        } else {
            alert('请填写手机号码')
        }
    })

    // 完成绑卡
    $(".next-btn").on('click', function () {
        var info = checkInput('.step-two')
        if (info.status) {
            $.ajax({
                url: 'https://healthcarddemo.tengmed.com/index.php?c=healthcard&a=createHealthCard',
                data: info.data,
                dataType: 'json',
                type: 'post',
                success: function (res) {
                    if (res.retcode === 0) {
                        window.location.href = "list.html"
                    } else if (res.retcode === 302) {
                        window.location.href = res.redirect_uri
                    } else if (res.retcode === 10010 || res.retcode === 10011) {
                        var r = confirm(res.retmsg)
                        if (r == true) {
                            window.location.href = 'https://healthcarddemo.tengmed.com/index.php?c=healthcard&a=authwechatCode'
                        }
                    } else {
                        alert(JSON.stringify(res))
                    }
                }
            })
        }
    })

})()

function checkInput(fa) {
    var phone = $(fa + " .phone").val()
    var smsCode = $(fa + " .validnumber").val()
    if (phone && smsCode) {
        $(fa + " .next-btn").removeAttr('disabled')
        return { status: true, data: { phone, smsCode } }
    } else {
        $(fa + " .next-btn").attr('disabled', "disabled")
        return { status: false, data: { phone, smsCode } }
    }
}

function auth() {
    var login = getCookie('isLogin') || ''
    var AuthCode = getCookie('isAuthCode') || ''
    if (login === '') {
        window.location.href = 'https://healthcarddemo.tengmed.com/index.php?c=healthcard&a=login'
        return false
    } else if (AuthCode === '') {
        window.location.href = 'https://healthcarddemo.tengmed.com/index.php?c=healthcard&a=authwechatCode'
        return false
    } else {
        return true
    }
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=")
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1
            c_end = document.cookie.indexOf(";", c_start)
            if (c_end == -1) {
                c_end = document.cookie.length
            }
            return unescape(document.cookie.substring(c_start, c_end))
        }
    }
    return ""
}
